<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('clock_in_records', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();

            // 打卡信息
            $table->date('clock_date');
            $table->datetime('clock_time');
            $table->string('clock_type', 20); // check_in, check_out, break_start, break_end
            $table->string('clock_method', 20)->default('online'); // online, excel_import, machine, auto

            // 位置信息
            $table->decimal('latitude', 10, 8)->nullable();
            $table->decimal('longitude', 11, 8)->nullable();
            $table->string('location_name', 200)->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->string('device_info', 200)->nullable();

            // 打卡验证
            $table->boolean('is_valid')->default(true);
            $table->string('validation_status', 20)->default('pending'); // pending, approved, rejected
            $table->text('validation_reason')->nullable();
            $table->string('validated_by', 100)->nullable();
            $table->datetime('validated_at')->nullable();

            // 地理围栏
            $table->unsignedBigInteger('geofence_id')->nullable();
            $table->string('geofence_name', 100)->nullable();
            $table->boolean('is_within_geofence')->nullable();

            // 异常标记
            $table->boolean('is_exception')->default(false);
            $table->string('exception_type', 50)->nullable(); // outside_geofence, wrong_location, duplicate, etc.
            $table->text('exception_reason')->nullable();

            // 补卡信息
            $table->boolean('is_makeup')->default(false);
            $table->datetime('original_time')->nullable();
            $table->text('makeup_reason')->nullable();

            // 设备信息
            $table->string('user_agent', 500)->nullable();
            $table->string('browser_info', 200)->nullable();
            $table->string('os_info', 100)->nullable();

            // 照片验证
            $table->string('photo_path', 500)->nullable();
            $table->boolean('face_recognition_used')->default(false);

            // 备注
            $table->text('notes')->nullable();
            $table->string('recorded_by', 100)->nullable();

            // 索引
            $table->index(['employee_id', 'clock_date']);
            $table->index(['clock_date', 'clock_type']);
            $table->index(['clock_method']);
            $table->index(['is_valid', 'validation_status']);
            $table->index(['is_exception', 'exception_type']);
            $table->index(['latitude', 'longitude']);
            $table->index(['geofence_id']);

            // 外键约束
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('geofence_id')->references('id')->on('geofences')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('clock_in_records');
    }
};
